var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var db = require("./models/db.js");
var bodyParser = require('body-parser');


var indexRouter = require('./routes/index');
var users = require('./routes/users');
var banner = require('./routes/banner');
var typelist = require('./routes/typelist');
var goodslist = require('./routes/goodslist');
var floor = require('./routes/floor');
var recommend = require('./routes/recommend');
var orders = require('./routes/orders');
var logistics = require('./routes/logistics');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(bodyParser.json({limit: '10mb'})); // for parsing application/json
app.use(bodyParser.urlencoded({limit: '10mb', extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', users);
app.use('/banner', banner);
app.use('/typelist', typelist);
app.use('/typelist/upload',express.static(path.join(__dirname, 'upload')))
app.use('/goodslist', goodslist);
app.use('/floor', floor);
app.use('/recommend', recommend);
app.use('/order', orders);
app.use('/logistics', logistics);



// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
